package com.gxuwz.check.action.admin;

import com.alibaba.fastjson.JSONObject;
import com.gxuwz.check.base.BaseAction;
import com.gxuwz.check.model.entity.SysCourseEntity;
import com.gxuwz.check.model.vo.CourseVO;
import com.gxuwz.check.model.vo.TeacherVO;
import com.gxuwz.check.service.admin.ICourseService;
import com.gxuwz.check.util.ResultJson;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class CourseAction extends BaseAction implements ModelDriven<SysCourseEntity> {
    @Override
    public SysCourseEntity getModel() {
        return null;
    }
    @Autowired
    private ICourseService courseService;

    private int page;

    private int collegeId;

    private int chooseType; //选择类型
    private String courseName; //课程名称
    private String teacherIdArray; //课程老师(可能多个)

    private Integer courseId;

    /**
     * 获取所有课程
     */
    public void list() {
        List<SysCourseEntity> list = courseService.list(page);
        List<CourseVO> listVO = new ArrayList<>();
        for (int i = 0 ; i < list.size() ; i ++){
            CourseVO courseVO = new CourseVO();
            courseVO.setCourseId(list.get(i).getCourseId());
            courseVO.setCourseName(list.get(i).getCourseName());
            if (list.get(i).getChooseType() == 1){
                courseVO.setChooseType("必修");
            }else {
                courseVO.setChooseType("选修");
            }
            listVO.add(courseVO);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        jsonObject.put("list",listVO);
        ResultJson.R(jsonObject);
    }

    /**
     * 删除课程
     */
    public void delete() {
        courseService.delete(courseId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        ResultJson.R(jsonObject);
    }

    /**
     * 修改课程
     */
    public void edit() {

    }

    /**
     * 添加课程
     */
    public void save() {
        System.out.println("保存方法------------------------------");
        courseService.save(chooseType,courseName,teacherIdArray);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        ResultJson.R(jsonObject);
    }

    /**
     *根据二级学院编号查询二级学院老师
     * @return
     */
    public void listTeacherByCollegeId(){
        List<TeacherVO> list = courseService.listTeacherByCollegeId(collegeId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",20000);
        jsonObject.put("list",list);
        ResultJson.R(jsonObject);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getCollegeId() {
        return collegeId;
    }

    public void setCollegeId(int collegeId) {
        this.collegeId = collegeId;
    }

    public int getChooseType() {
        return chooseType;
    }

    public void setChooseType(int chooseType) {
        this.chooseType = chooseType;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getTeacherIdArray() {
        return teacherIdArray;
    }

    public void setTeacherIdArray(String teacherIdArray) {
        this.teacherIdArray = teacherIdArray;
    }

    public Integer getCourseId() {
        return courseId;
    }

    public void setCourseId(Integer courseId) {
        this.courseId = courseId;
    }
}
